package com.sstock.smartstock;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class FileOperation {
	
	static String tempString;

	/**
	 * write something to a file
	 */
	public static void writeContentStringArray(String fileName, String[] content){
    	try {
    		FileWriter writer = new FileWriter(fileName);
    		
    		for(String arg:content){
    			if(arg != null)
    			{
    				writer.write(arg);
    				writer.write('\n');
    			}
    			else
    			{
    				break;
    			}
    		}
    		
    		writer.close();
    		
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
	
	/**
	 * write something to a file just String
	 */
	public static void writeContentString(String fileName, String content){
    	try {
    		FileWriter writer = new FileWriter(fileName);
    		
    		Log.v("Content is ", content);
    		writer.write(content);
    		
    		writer.write('\n');
    		
    		writer.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
	
	/**
	 * read File by Lines
	 */
	public static String readFileByLines(String fileName){
		File file = new File(fileName);
		BufferedReader reader = null;
		tempString = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String temp = null;
			while ((temp = reader.readLine()) != null){ 
			       tempString = temp;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null){
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return tempString;
	}
	
	/**
	 * delete file
	 */
	public static boolean delFile(String filePathAndName) {
		  boolean bool = false;
		  try {
		   String filePath = filePathAndName;
		   File myDelFile = new File(filePath);
		   if (myDelFile.exists()) {
		    myDelFile.delete();
		    bool = true;
		   } else {
		    bool = false;
		   }
		  } catch (Exception e) {
			 e.printStackTrace(); 
		  }
		  return bool;
		 }

	/**
	 * if there is no version used to made version.txt
	 * @throws IOException 
	 */
	static boolean makeVersionText() throws IOException {
		
		String dir = "";
		dir = Environment.getExternalStorageDirectory() + "/stocklist/";
		File f = new File(dir);
		if(!(f.exists()) && !(f.isDirectory()))
		{
			f.mkdirs();
		}
		
		String newVersion = "version.txt";
		newVersion = dir + newVersion;
		File newText = new File(newVersion);
		if(!newText.exists())
		{
			newText.createNewFile();
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * test one file is exist or not
	 */
	static boolean isThereTheFile(String fileName) {
		
		File f = new File(fileName);
		
		if(f.exists())
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	

}
